<template>
  <el-dialog
    :title="!dataForm.culturalProductNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文创id" prop="culturalProductId">
      <el-input v-model="dataForm.culturalProductId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="文创名字" prop="culturalProductName">
      <el-input v-model="dataForm.culturalProductName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="文创介绍" prop="culturalProductContent">
      <el-input v-model="dataForm.culturalProductContent" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="文创图片" prop="culturalProductImg">
      <el-input v-model="dataForm.culturalProductImg" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCulturalProductById, saveOrUpdateCulturalProduct } from '@/api/culturalproduct'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        culturalProductNo: 0,
        culturalProductId: '',
        culturalProductName: '',
        culturalProductContent: '',
        culturalProductImg: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        culturalProductId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        culturalProductName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        culturalProductContent: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        culturalProductImg: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.culturalProductNo = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.culturalProductNo) {
          const { data } = await getCulturalProductById(this.dataForm.culturalProductNo)
          if (data && data.code === 0) {
            this.dataForm.culturalProductId = data.culturalProduct.culturalProductId
            this.dataForm.culturalProductName = data.culturalProduct.culturalProductName
            this.dataForm.culturalProductContent = data.culturalProduct.culturalProductContent
            this.dataForm.culturalProductImg = data.culturalProduct.culturalProductImg
            // this.dataForm.createTime = data.culturalProduct.createTime
            // this.dataForm.updateTime = data.culturalProduct.updateTime
          }

          // this.$http({
          //   url: `/culturalproduct/info/${this.dataForm.culturalProductNo}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.culturalProductId = data.culturalProduct.culturalProductId
          //     this.dataForm.culturalProductName = data.culturalProduct.culturalProductName
          //     this.dataForm.culturalProductContent = data.culturalProduct.culturalProductContent
          //     this.dataForm.culturalProductImg = data.culturalProduct.culturalProductImg
          //     // this.dataForm.createTime = data.culturalProduct.createTime
          //     // this.dataForm.updateTime = data.culturalProduct.updateTime
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const culturalProduct = {
            culturalProductNo: this.dataForm.culturalProductNo || undefined,
            culturalProductId: this.dataForm.culturalProductId,
            culturalProductName: this.dataForm.culturalProductName,
            culturalProductContent: this.dataForm.culturalProductContent,
            culturalProductImg: this.dataForm.culturalProductImg
            // createTime: this.dataForm.createTime,
            // updateTime: this.dataForm.updateTime
          }
          const { data } = await saveOrUpdateCulturalProduct(this.dataForm.culturalProductNo, culturalProduct)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/culturalproduct/${!this.dataForm.culturalProductNo ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     culturalProductNo: this.dataForm.culturalProductNo || undefined,
          //     culturalProductId: this.dataForm.culturalProductId,
          //     culturalProductName: this.dataForm.culturalProductName,
          //     culturalProductContent: this.dataForm.culturalProductContent,
          //     culturalProductImg: this.dataForm.culturalProductImg
          //     // createTime: this.dataForm.createTime,
          //     // updateTime: this.dataForm.updateTime
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
